<?php

namespace app\miniapp\controller\v1;


use app\common\cachemodel\MiniAppUser;
use app\common\lib\IAuth;
use think\Controller;

class Test extends Controller
{
    public function index()
    {
        // $id = input('id/d', 1);
        // $token = IAuth::setAppLoginToken($id);
        // $data = [
        //     'access_token' => $token,
        //     'expires_in' => strtotime('+' . config('miniapp.login_time_out_day') . ' days'),
        // ];
        //
        // (new MiniAppUser())->save($data, ['id' => $id]);
        //
        // $access_token = $this->encryptWithOpenssl($token . "||" . $id);
        //
        // return show(0, '更新成功', ['access_token' => $access_token]);
    }


    /**
     * 加密字符串
     * @param string $data 字符串
     * @return string
     */
    protected function encryptWithOpenssl($data): string
    {
        $config = config('miniapp');
        return base64_encode(openssl_encrypt($data, $config['aes_method'], $config['aes_key'], OPENSSL_RAW_DATA, $config['aes_iv']));
    }

    /**
     * 解密字符串
     * @param string $data 字符串
     * @return string
     */
    protected function decryptWithOpenssl($data): string
    {
        $config = config('miniapp');
        return openssl_decrypt(base64_decode($data), $config['aes_method'], $config['aes_key'], OPENSSL_RAW_DATA, $config['aes_iv']);
    }
}
